# docker build --build-arg user=xxx --build-arg jupyter=1 -t xxx/minidev .
# docker create -p 8000:8000 -p 8001:8001 -p 8002:8002 -p 8003:8003 -p 8004:8004 -p 8005:8005 -p 8006:8006 -p 8007:8007 --name minidev -v /var/run/docker.sock:/var/run/docker.sock xiqp/minidev

FROM alpine:latest
EXPOSE 8000 8001 8002 8003
VOLUME ["/work"]

ARG user=minidev
ARG jupyter=0
ARG userid=1000

ENV WORKUSER $user
ENV JUPYTER_ENABLE $jupyter

ADD rootfs / 

RUN \
    dos2unix /init `find /home/user -type f -exec grep -Iq . {} \; -and -print` && \
    chmod +x /init /dumb-init && \
    sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories

RUN \
    apk add --no-cache --update \
        bash curl openssh-server openssh-sftp-server tmux sudo htop \
        g++ musl-dev file git vim python3-dev go make nodejs-npm \
        && \
    \
    ln -sf bash /bin/sh && \
    ln -sf vim /usr/bin/vi && \
    ssh-keygen -A && \
    \
    adduser -D -h /home/${user} -g users -u ${userid} -s /bin/bash ${user} && \
    echo "${user}:${user}" | chpasswd && \
    mkdir -p /etc/sudoers.d && \
    echo "${user} ALL=(ALL) NOPASSWD: ALL" >/etc/sudoers.d/${user} && \
    chmod 0440 /etc/sudoers.d/${user} && \
    \
    rm -rf /home/${user} && \
    mv /home/user /home/${user} && \
    chown -R ${user}:users /home/${user}

USER ${user}

RUN \
    cd /home/${user} && \
    ln -sf /dev/null .bash_history && \
    git clone https://gitee.com/xiqingping/vimconfig .vim

RUN \
    if [ "$jupyter" = "1" ]; then \
        cd /home/${user} && \
        pip3 install --user jupyterlab && \
        ~/.local/bin/jupyter serverextension enable --py jupyterlab; \
    fi

USER root

RUN \
    mkdir -p /run && \
    rm -rf /var/tmp /var/log /var/cache /tmp /var/run && \
    ln -sf /run/_var/tmp /var/tmp && \
    ln -sf /run/_var/run /var/run && \
    ln -sf /run/_var/cache /var/cache && \
    ln -sf /run/_var/log /var/cache && \
    ln -sf /run/_tmp /tmp

ENTRYPOINT ["/dumb-init", "--"]
CMD ["/init"]

